package com.ht.system.service.impl;

import java.util.Date;
import java.util.List;
import javax.validation.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ht.system.mapper.EnterpriseFieldAuthMapper;
import com.ht.common.core.enums.EntAuditStatusEnum;
import com.ht.common.core.exception.ServiceException;
import com.ht.common.security.utils.SecurityUtils;
import com.ht.system.domain.EnterpriseFieldAuth;
import com.ht.system.service.IEnterpriseFieldAuthService;

/**
 * 企业场所授权Service业务层处理
 *
 * @author zmx
 * @date 2025-03-23
 */
@Service
public class EnterpriseFieldAuthServiceImpl implements IEnterpriseFieldAuthService
{
    @Autowired
    private EnterpriseFieldAuthMapper enterpriseFieldAuthMapper;

    /**
     * 查询企业场所授权
     *
     * @param id 企业场所授权主键
     * @return 企业场所授权
     */
    @Override
    public EnterpriseFieldAuth selectEnterpriseFieldAuthById(String id)
    {
        return enterpriseFieldAuthMapper.selectEnterpriseFieldAuthById(id);
    }

    /**
     * 查询企业场所授权列表
     *
     * @param enterpriseFieldAuth 企业场所授权
     * @return 企业场所授权
     */
    @Override
    public List<EnterpriseFieldAuth> selectEnterpriseFieldAuthList(EnterpriseFieldAuth enterpriseFieldAuth)
    {
        return enterpriseFieldAuthMapper.selectEnterpriseFieldAuthList(enterpriseFieldAuth);
    }

    /**
     * 新增企业场所授权
     *
     * @param enterpriseFieldAuth 企业场所授权
     * @return 结果
     */
    @Override
    public int insertEnterpriseFieldAuth(EnterpriseFieldAuth enterpriseFieldAuth)
    {
    	enterpriseFieldAuth.setApplyTime(new Date());
    	enterpriseFieldAuth.setAuthStatus(EntAuditStatusEnum.NO_AUDIT.getCode());
        return enterpriseFieldAuthMapper.insertEnterpriseFieldAuth(enterpriseFieldAuth);
    }

    /**
     * 修改企业场所授权
     *
     * @param enterpriseFieldAuth 企业场所授权
     * @return 结果
     */
    @Override
    public int updateEnterpriseFieldAuth(EnterpriseFieldAuth enterpriseFieldAuth)
    {
    	EnterpriseFieldAuth efa=enterpriseFieldAuthMapper.selectEnterpriseFieldAuthById(enterpriseFieldAuth.getId());
		if(null==efa){
			throw new ServiceException("记录不存在");
		}
		if(EntAuditStatusEnum.AUDIT_PASS.getCode().equals(efa.getAuthStatus())){
			throw new ServiceException("当前记录已认证通过");
		}
		efa.setGenBillsMark(enterpriseFieldAuth.getGenBillsMark());
		efa.setDeclBillsMark(enterpriseFieldAuth.getDeclBillsMark());
		efa.setOsdMark(enterpriseFieldAuth.getOsdMark());
		efa.setQueryDataMark(enterpriseFieldAuth.getQueryDataMark());
		efa.setApplyNote(enterpriseFieldAuth.getApplyNote());
		efa.setApplyTime(new Date());
        return enterpriseFieldAuthMapper.updateEnterpriseFieldAuth(efa);
    }

    /**
     * 批量删除企业场所授权
     *
     * @param ids 需要删除的企业场所授权主键
     * @return 结果
     */
    @Override
    public int deleteEnterpriseFieldAuthByIds(@NotEmpty(message="参数不能为空") String[] ids)
    {

    	int delRes=0;
    	for (String id : ids) {
    		delRes+=deleteEnterpriseFieldAuthById(id);
		}
        return delRes;
    }

    /**
     * 删除企业场所授权信息
     *
     * @param id 企业场所授权主键
     * @return 结果
     */
    public int deleteEnterpriseFieldAuthById(String id)
    {
        return enterpriseFieldAuthMapper.deleteEnterpriseFieldAuthById(id);
    }

	@Override
	public int auditEnterpriseFieldAuth(EnterpriseFieldAuth enterpriseFieldAuth) {
		EnterpriseFieldAuth efa=enterpriseFieldAuthMapper.selectEnterpriseFieldAuthById(enterpriseFieldAuth.getId());
		if(null==efa){
			throw new ServiceException("记录不存在");
		}
		if(EntAuditStatusEnum.AUDIT_PASS.getCode().equals(efa.getAuthStatus())){
			throw new ServiceException("当前记录已认证通过");
		}
		efa.setAuther(SecurityUtils.getNickName());
		efa.setAuthRemark(enterpriseFieldAuth.getAuthRemark());
		efa.setAuthStatus(EntAuditStatusEnum.AUDIT_PASS.getCode());
		efa.setAuthTime(new Date());
		return enterpriseFieldAuthMapper.updateEnterpriseFieldAuth(efa);
	}

    @Override
    public List<EnterpriseFieldAuth> selectByEntCode(String entCode) {
        return enterpriseFieldAuthMapper.selectByEntCode(entCode);
    }
}
